from typing import Any, Optional
from fastapi.responses import JSONResponse
from fastapi.encoders import jsonable_encoder


class UTF8JSONResponse(JSONResponse):
    media_type = "application/json; charset=utf-8"


def success_response(
    data: Any = None, 
    message: str = "操作成功", 
    code: int = 200
) -> UTF8JSONResponse:
    return UTF8JSONResponse(
        status_code=200,
        content={
            "code": code,
            "message": message,
            "data": jsonable_encoder(data)
        }
    )


def error_response(
    message: str = "操作失败", 
    code: int = 500,
    status_code: Optional[int] = None,
    data: Any = None
) -> UTF8JSONResponse:
    return UTF8JSONResponse(
        status_code=status_code or code,
        content={
            "code": code,
            "message": message,
            "data": jsonable_encoder(data)
        }
    ) 